// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: GHZ Setup Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

private alias Object.Value0 : Object.Timer
private alias Object.Value1 : Attack.Timer
private alias Object.Value2 : Mercy.Attack
private alias Object.Value3 : Wait.Time

private alias Object[40].Value33 : Object.AttackTimer
private alias Object[40].Value28 : Object.TableContainer
private alias object.value6 : player.invulnerable


reserve function DeltaAttacks_SummonMercy

// State aliases
private alias 0  : DELTAATTACKS_PROJECTILE_0
private alias 1  : DELTAATTACKS_PROJECTILE_MERCY_1
private alias 2  : DELTAATTACKS_THUNDERALERT_2
private alias 3  : DELTAATTACKS_FASTTHUNDER_3
private alias 4  : DELTAATTACKS_TREE_UP_4
private alias 5  : DELTAATTACKS_TREE_DOWN_5
private alias 6  : DELTAATTACKS_TREE_RIGHT_6
private alias 7  : DELTAATTACKS_TREE_LEFT_7
private alias 8  : DELTAATTACKS_TREE_L_ALT_8

public value Delta_HP = 90;
public value Delta_YourTurn = 1;
public value Delta_SlenderMercy = 0;

function DeltaAttacks_SummonMercy

	Attack.Timer++
	if Attack.Timer == 100
		temp1 = screen.xcenter
		temp1 *= 0x19000
		temp2 = Screen.ycenter
		temp2 *= 0x10000

	
		CreateTempObject(TypeName[Delta Attacks], 1, temp1, temp2)
		object[tempObjectPos].state = 2
		object[tempObjectPos].drawOrder = 6	
		
		Attack.Timer = 0
	end if

end function

event ObjectMain
if object[40].state == 6
	Object.Priority = PRIORITY_ACTIVE
	object.drawOrder = 6
	Object.Xpos += Object.xvel
	Object.Ypos += Object.Yvel
	switch object.propertyValue
	case DELTAATTACKS_PROJECTILE_0
	case DELTAATTACKS_PROJECTILE_MERCY_1
		switch object.state
		case 0

			if Delta_AttackHardMode == 1
				Wait.Time = 75
			else
				Wait.Time = 100
			end if

			Attack.Timer++
			if Attack.Timer >= Wait.Time
				temp1 = screen.xcenter
				temp1 *= 0x19000
				temp2 = Screen.ycenter
				temp2 *= 0x10000

			
				if Delta_BecomeGreen == 1
					rand(Mercy.Attack, 4)		//4 =	0, 1, 2, 3
					if Mercy.Attack == 3	//25% chance
						Mercy.Attack = 1
					end if
				end if
			
				
				CreateTempObject(TypeName[Delta Attacks], 0, temp1, temp2)
				if Mercy.Attack == 1
					object.state = 2
				else
					object.state++	
				end if
				object[tempObjectPos].drawOrder = 6	
				
				Attack.Timer = 0
			end if			
		case 1	//normal evil attack
			object.timer++

			if object.timer <= 200
				CheckGreater(object.xpos, object[currentPlayer].xpos)
				temp0 = checkResult
				if temp0 == 1		//right
					if object.xvel > 0
						object.xvel -= 0x1000	//right
						if object.xvel > 0x4000
							object.xvel = 0x4000
						end if
					else
						object.xvel -= 0x500	//left?? i have no clue
						if object.xvel > 0x5000
							object.xvel = 0x5000
						end if			
					end if
				else	//left
					if object.xvel < 0
						object.xvel += 0x2000
						if object.xvel > 0x4000
							object.xvel = 0x4000
						end if		
					else
						object.xvel += 0x1500
						if object.xvel > 0x4000
							object.xvel = 0x4000
						end if								
					end if
				end if

				CheckGreater(object.ypos, object[currentPlayer].ypos)
				temp0 = checkResult

				if temp0 == 1		//right
					if object.yvel > 0
						object.yvel -= 0x2000	
					else
						object.yvel -= 0x1500	
					end if		
				else		//down
					if object.yvel < 0
						object.yvel += 0x2000
					else
						object.yvel += 0x1500
					end if
				end if	
			end if


			if object.timer == 400
				object.type = TypeName[Blank Object]
				object.timer = 0	
			end if

		break
		case 2	//happy attack
			CheckGreater(object.xpos, object[currentPlayer].xpos)
			temp0 = checkResult
			if temp0 == 1		//right
				if object.xvel > 0
					object.xvel -= 0x1000	//right
					if object.xvel > 0x4000
						object.xvel = 0x4000
					end if
				else
					object.xvel -= 0x500	//left?? i have no clue
					if object.xvel > 0x5000
						object.xvel = 0x5000
					end if			
				end if
			else	//left
				if object.xvel < 0
					object.xvel += 0x2000
					if object.xvel > 0x4000
						object.xvel = 0x4000
					end if		
				else
					object.xvel += 0x1500
					if object.xvel > 0x4000
						object.xvel = 0x4000
					end if								
				end if
			end if

			CheckGreater(object.ypos, object[currentPlayer].ypos)
			temp0 = checkResult

			if temp0 == 1		//right
				if object.yvel > 0
					object.yvel -= 0x2000	
				else
					object.yvel -= 0x1500	
				end if		
			else		//down
				if object.yvel < 0
					object.yvel += 0x2000
				else
					object.yvel += 0x1500
				end if
			end if	
		break			
		end switch
		

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)	
			if object.state == 2
				BoxCollisionTest(C_TOUCH, object.entityPos, -6, -6, 6, 6, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					Delta_SlenderMercy += 5
					if ringPan == 0
						PlaySfx(SfxName[Ring L], 0)
						SetSfxAttributes(SfxName[Ring L], -1, -100)
						ringPan = 1
					else
						PlaySfx(SfxName[Ring R], 0)
						SetSfxAttributes(SfxName[Ring R], -1, 100)
						ringPan = 0
					end if			
					Delta_ShowMercy = 1			
					Object.TableContainer = 0
					object.type = TypeName[Blank Object]	
				end if
			else
				if object[currentPlayer].value6 == 0
					BoxCollisionTest(C_TOUCH, object.entityPos, -6, -6, 6, 6, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						if Delta_Defending == 1
							Delta_HP--
						else
							Delta_HP -= 2
						end if
						PlaySfx(SfxName[Hurt], 0)
						object[40].AnimationTimer = 80
						object[40].Animation = 2
						object[currentPlayer].value6	= 80
					end if
				end if
			end if
		next		
	break
	case DELTAATTACKS_THUNDERALERT_2
	case DELTAATTACKS_FASTTHUNDER_3
		if Delta_AttackHardMode == 1
			Attack.Timer = 15
		else	
			Attack.Timer = 25
		end if

		switch object.state
			case 0	//spawning in,	
				object.timer++
				Object.Xpos = object[currentPlayer].xpos
				temp0 = screen.ycenter
				temp0 *= 0x9900
				Object.Ypos = temp0
				if object.timer == Attack.Timer
					playsfx(SfxName[Warning], 0)
					object.state++
					object.timer = 0
				end if
			break
			case 1	//wait a sec
				object.timer++
				if object.timer == Attack.Timer	
					playsfx(SfxName[SlenderThunder], 0)

					if Delta_BecomeGreen == 1
						rand(Mercy.Attack, 4)		//4 =	0, 1, 2, 3
						if Mercy.Attack == 3	//25% chance
							Mercy.Attack = 1
						end if
					end if				
					if mercy.attack == 1	
						object.state = 3
					else
						object.state++
					end if
					object.timer = 0
				end if
			break
			case 2
				object.timer++
				if object.timer == Attack.Timer
					object.state = 0
					object.timer = 0
				end if

				foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)	
					if object[currentPlayer].value6 == 0
						BoxCollisionTest(C_TOUCH, object.entityPos, -10, -6, 10, 71, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
						if checkResult == 1
							if Delta_Defending == 1
								Delta_HP-= 2
							else
								Delta_HP -= 4
							end if
							PlaySfx(SfxName[Hurt], 0)
							object[40].AnimationTimer = 80
							object[40].Animation = 2
							object[currentPlayer].value6	= 80
						end if
					end if
				next
			break
			case 3
				object.timer++
				temp0 = Attack.Timer
				temp0 *= 3
				if object.timer == temp0
					object.state = 0
					object.timer = 0
				end if

				foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)	
					BoxCollisionTest(C_TOUCH, object.entityPos, -10, -6, 10, 71, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						Delta_SlenderMercy+= 5
						if ringPan == 0
							PlaySfx(SfxName[Ring L], 0)
							SetSfxAttributes(SfxName[Ring L], -1, -100)
							ringPan = 1
						else
							PlaySfx(SfxName[Ring R], 0)
							SetSfxAttributes(SfxName[Ring R], -1, 100)
							ringPan = 0
						end if			
						Delta_ShowMercy = 1			
						Object.TableContainer = 0

						temp1 = screen.xcenter
						temp1 *= 0x19000
						temp2 = Screen.ycenter
						temp2 *= 0x10000
						//what?
						Mercy.Attack = 0
						if object.propertyValue == 3
							CreateTempObject(TypeName[Delta Attacks], 3, temp1, temp2)
						else
							CreateTempObject(TypeName[Delta Attacks], 2, temp1, temp2)
						end if	
						object[tempObjectPos].Value2 = 0

						object.type = TypeName[Blank Object]
					end if
				next
			break			
		end switch
	break
	case DELTAATTACKS_TREE_UP_4
		switch object.state 
			case 0
				object.timer++
				Object.Xpos = object[currentPlayer].xpos

				object.alpha += 4

				if object.timer >= Wait.Time
					object.state++
					PlaySfx(SfxName[Release],0)
					object.timer = 0
					object.alpha = 255
				end if
			break
			case 1
				object.ypos += object.yvel
				object.yvel -= 0x1600

				if object.outofbounds == 1
					temp1 = screen.xcenter
					temp1 *= 0x19000
					temp2 = Screen.ycenter
					temp2 *= 0x10000

					CreateTempObject(TypeName[Delta Attacks], 4, temp1, temp2)
					object[tempObjectPos].state = 0
					object[tempObjectPos].alpha = 0
					object[tempObjectPos].value3 = 75
					object[tempObjectPos].drawOrder = 6	
					object.type = TypeName[Blank Object]
				end if
			break			
		end switch

		if Delta_BecomeGreen == 1
			Callfunction(DeltaAttacks_SummonMercy)
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)	
			BoxCollisionTest(C_TOUCH, object.entityPos, -12, 24, 12, 84, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if object[currentPlayer].value6 == 0
				if checkResult == 1
					if Delta_Defending == 1
						Delta_HP-= 1
					else
						Delta_HP -= 2
					end if
					PlaySfx(SfxName[Hurt], 0)
					object[40].AnimationTimer = 80
					object[40].Animation = 2
					object[currentPlayer].value6	= 80
				end if
			end if
		next
	break
	case DELTAATTACKS_TREE_DOWN_5
		switch object.state 
			case 0
				object.timer++
				Object.Xpos = object[currentPlayer].xpos
				temp2 = -0x200000
				Object.Ypos = temp2

				object.alpha += 4

				if object.timer >= Wait.Time
					object.state++
					PlaySfx(SfxName[Release],0)
					object.timer = 0
					object.alpha = 255
				end if
			break
			case 1
				object.timer++

				object.ypos += object.yvel
				object.yvel += 0x1600


				if object.outofbounds == 1
					temp1 = screen.xcenter
					temp1 *= 0x19000
					temp2 = Screen.ycenter
					temp2 *= 0x10000

				
					CreateTempObject(TypeName[Delta Attacks], 5, temp1, temp2)
					object[tempObjectPos].state = 0
					object[tempObjectPos].alpha = 0
					object[tempObjectPos].value3 = 75
					object[tempObjectPos].drawOrder = 6	
					object.type = TypeName[Blank Object]
				end if
			break			
		end switch

		if Delta_BecomeGreen == 1
			Callfunction(DeltaAttacks_SummonMercy)
		end if		

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)	
			BoxCollisionTest(C_TOUCH, object.entityPos, -12, 24, 12, 84, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if object[currentPlayer].value6 == 0
				if checkResult == 1
					if Delta_Defending == 1
						Delta_HP-= 1
					else
						Delta_HP -= 2
					end if
					PlaySfx(SfxName[Hurt], 0)
					object[40].AnimationTimer = 80
					object[40].Animation = 2
					object[currentPlayer].value6	= 80
				end if
			end if
		next

	break
	case DELTAATTACKS_TREE_RIGHT_6
		switch object.state 
			case 0
				object.timer++
				Object.Ypos = object[currentPlayer].Ypos
				temp2 = 0x400000
				Object.Xpos = temp2

				object.alpha += 4

				if object.timer >= Wait.Time
					object.state++
					PlaySfx(SfxName[Release],0)
					object.timer = 0
					object.alpha = 255
				end if
			break
			case 1
				object.timer++

				object.xpos += object.xvel
				object.xvel += 0x2000


				if object.outofbounds == 1
					temp1 = screen.xcenter
					temp1 *= 0x19000
					temp2 = Screen.ycenter
					temp2 *= 0x10000

				
					CreateTempObject(TypeName[Delta Attacks], 7, temp1, temp2)
					object[tempObjectPos].state = 0
					object[tempObjectPos].alpha = 0
					object[tempObjectPos].value3 = 25
					object[tempObjectPos].drawOrder = 6	
					object.type = TypeName[Blank Object]
				end if
			break				
		end switch

		if Delta_BecomeGreen == 1
			Callfunction(DeltaAttacks_SummonMercy)
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)	
			BoxCollisionTest(C_TOUCH, object.entityPos, 24, -12, 84, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if object[currentPlayer].value6 == 0
				if checkResult == 1
					if Delta_Defending == 1
						Delta_HP-= 1
					else
						Delta_HP -= 2
					end if
					PlaySfx(SfxName[Hurt], 0)
					object[40].AnimationTimer = 80
					object[40].Animation = 2
					object[currentPlayer].value6	= 80
				end if
			end if
		next

	break
	case DELTAATTACKS_TREE_LEFT_7

		switch object.state 
			case 0
				object.timer++
				Object.Ypos = object[currentPlayer].Ypos
				temp2 = 16000000
				Object.Xpos = temp2

				object.alpha += 4

				if object.timer >= Wait.Time
					object.state++
					PlaySfx(SfxName[Release],0)
					object.timer = 0
					object.alpha = 255
				end if
			break
			case 1
				object.timer++

				object.xpos += object.xvel
				object.xvel -= 0x2000


				if object.outofbounds == 1
					temp1 = screen.xcenter
					temp1 *= 0x19000
					temp2 = Screen.ycenter
					temp2 *= 0x10000

				
					CreateTempObject(TypeName[Delta Attacks], 6, temp1, temp2)
					object[tempObjectPos].state = 0
					object[tempObjectPos].alpha = 0
					object[tempObjectPos].value3 = 25
					object[tempObjectPos].drawOrder = 6	
					object.type = TypeName[Blank Object]
				end if
			break				
		end switch

		if Delta_BecomeGreen == 1
			Callfunction(DeltaAttacks_SummonMercy)
		end if


		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)	
			BoxCollisionTest(C_TOUCH, object.entityPos, 24, -12, 84, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if object[currentPlayer].value6 == 0
				if checkResult == 1
					if Delta_Defending == 1
						Delta_HP-= 1
					else
						Delta_HP -= 2
					end if
					PlaySfx(SfxName[Hurt], 0)
					object[40].AnimationTimer = 80
					object[40].Animation = 2
					object[currentPlayer].value6	= 80
				end if
			end if
		next

	break	
	end switch
else
	if object[40].state > 6
		object.type = TypeName[Blank Object]
	end if
end if
end event

event ObjectDraw
if object[40].state == 6
	switch object.propertyValue
	case 0
	case 1
		CheckEqual(Mercy.Attack, 1)
		temp1 = checkResult
		CheckEqual(object.state, 2)
		temp1 |= checkResult			
		if temp1 == 1
			DrawSpriteXY(1, Object.Xpos, Object.Ypos)
		else
			DrawSpriteXY(0, Object.Xpos, Object.Ypos)
		end if
	break
	case 2
	case 3
		switch object.state 
			case 1
				DrawSpriteXY(2, Object.Xpos, Object.Ypos)
			break
			case 2
				DrawSpriteXY(3, Object.Xpos, Object.Ypos)
			break			
			case 3
				DrawSpriteXY(4, Object.Xpos, Object.Ypos)
			break			
		end switch
	break
	case 4
		object.inkeffect = INK_ALPHA
		DrawSpriteFX(6, FX_INK, Object.Xpos, Object.Ypos)
	break
	case 5
		object.inkeffect = INK_ALPHA
		DrawSpriteFX(7, FX_INK, Object.Xpos, Object.Ypos)
	break	
	case 6
		object.inkeffect = INK_ALPHA
		DrawSpriteFX(8, FX_INK, Object.Xpos, Object.Ypos)
	break	
	case 7
	case 8
		object.inkeffect = INK_ALPHA
		DrawSpriteFX(9, FX_INK, Object.Xpos, Object.Ypos)
	break		
	end switch
end if
end event

event ObjectStartup
	LoadSpriteSheet("3D_Test/DeltaSheet.gif")
	SpriteFrame(-5, -5, 10, 10, 136, 49)	//Thing	0
	SpriteFrame(-5, -5, 10, 10, 147, 49)	//Green Page	0
	SpriteFrame(-10, -3, 20, 71, 415, 131)	//watch out 2
	SpriteFrame(-10, -3, 20, 71, 394, 131)	//thunda 3
	SpriteFrame(-10, -3, 20, 71, 373, 131)	//page... 4
	SpriteFrame(-36, 0, 73, 72, 353, 203)	//5 tree attack

	SpriteFrame(-12, 24, 24, 72, 328, 203)	//6 tree up
	SpriteFrame(-12, 24, 24, 72, 366, 301)	//7 tree down	
	SpriteFrame(24, -12, 72, 24, 354, 276)	//8 tree up
	SpriteFrame(24, -12, 72, 24, 391, 357)	//9 tree up		
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("IIZ/Objects1.gif")
	SpriteFrame(0, 0, 64, 64, 153, 154)
end event
